import { parseBlobData } from '@/utils/utils'

const ws = new WebSocket('ws://localhost:3000')

ws.onopen = function open() {
  console.log('connected')
}

ws.onmessage = async function incoming(data) {
  const getData = (await parseBlobData(data.data)) as any
  const message = JSON.parse(getData)
  if (message.type === 'connect') {
    console.log(message.content)
  } else if (message.type === 'disconnect') {
    console.log(message.content)
  } else {
    // 处理聊天内容
    console.log(message.content)
  }
}

ws.onclose = function close() {
  console.log('disconnected')
}

ws.onerror = function error(err) {
  console.error(err)
}

// 发送消息
function sendMessage(content: string) {
  ws.send(JSON.stringify({ type: 'chat', content }))
}
